class Solution
{
public:
    bool isBalanced(TreeNode* root)
    {
        if (root == nullptr) return true;
        return abs(depth(root->left) - depth(root->right)) <= 1
            && isBalanced(root->left)
            && isBalanced(root->right);
    }
private:
    int depth(TreeNode* root)
    {
        if (root == nullptr) return 0;
        return max(depth(root->left), depth(root->right)) + 1;
    }
};